//
//  ViewController.m
//  09-HomeWork
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "CityViewController.h"
#define kTableViewCellReuse @"reuseIdentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *provinceOftableView;
@property (nonatomic, strong) NSMutableArray *allProArray;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTableView];
    self.navigationItem.title = @"省份";
}
#pragma mark - 数据处理
- (void)handleData {
    // 取到地址
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-HomeWork/09-HomeWork/area.txt";
    // 取到字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 取出(根据"\n")截取的所有的字符串, 放入数组中
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    // 1. 省数组 - 省字典 - 市数组 - 市字典 - 区数组 - 区字典
    self.allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [_allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [_allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[_allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
//    NSLog(@"%@", _allProArray);
}
#pragma mark - 创建tableView
- (void)createTableView {
    self.provinceOftableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_provinceOftableView];
    _provinceOftableView.delegate = self;
    _provinceOftableView.dataSource = self;
}
#pragma mark - 实现协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allProArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewCellReuse];
    }
    Province *pro = [_allProArray objectAtIndex:indexPath.row];
    NSArray *arr = [pro.name componentsSeparatedByString:@" "];
    cell.textLabel.text = arr[0];
    cell.detailTextLabel.text = arr[1];
    return cell;
}
#pragma mark - 选中cell时触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityViewController *city = [[CityViewController alloc] init];
    Province *pro = [_allProArray objectAtIndex:indexPath.row];
    city.arrayOfCity = [NSArray arrayWithArray:pro.array];
    NSString *title = pro.name;
    NSArray *arr = [title componentsSeparatedByString:@" "];
    city.navigationItem.title = arr[0];
    [self.navigationController pushViewController:city animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
